<?php
global $AppUI, $m, $tab;

$currency = dPgetParam($_GET, 'currency', 0);
$customer = dPgetParam($_GET, 'customer', 0);
$shippingType = dPgetParam($_GET, 'shipping_type', 0);
$projectStartDate = dPgetParam($_GET, 'project_start_date', 0);
//// TODO: Check permission role
//$perms =& $AppUI->acl();
//// Check edit
//$denyEdit = !$perms->checkModuleItem($m, 'edit', $quote_id);
//// Check create
//$denyAdd = !$perms->checkModule($m, 'add');
//
//if($denyEdit and (0 < $quote_id)) {
//	$AppUI->setMsg('Edit Permission Deny', UI_MSG_ERROR);
//	$AppUI->redirect(PUBLIC_PAGE);
//}
//if($denyAdd){
//	$AppUI->setMsg('Add Permission Deny', UI_MSG_ERROR);
//	$AppUI->redirect(PUBLIC_PAGE);
//}
// Get id of active, complete
$active_key = dPgetSysVal(ACTIVE_KEY);
$comp_key = dPgetSysVal(COMPLETED_KEY);

// Get code of status
$objMD = new CMdStatus();
$objMD->loadByCode($active_key['code']);

$q = new DBQuery();
$q->dropTemp('vat_cus');

$q->createTemp('vat_cus');
$q->addTable('companies');
$q->innerJoin('classifications', 'cls', 'company_classification = cls_id');
$q->addQuery('company_id, cls_id, cls_vat_exemption');
$q->exec();
$q->clear();

$q = new DBQuery();
$q->addTable('quotation_lines');
$q->innerJoin('quotations', 'q', 'quote_id = qitem_quotation');
$q->innerJoin('vat_cus', 'v', 'v.company_id = quote_customer');

$q->addWhere('q.quote_status = ' . $objMD->status_id );
$q->addWhere("(q.quote_start_date  <= '$projectStartDate' and quote_expire_date >= '$projectStartDate')");
if ($customer > 0) {
	$q->addWhere("quote_customer = $customer");
}
if ($currency > 0) {
	$q->addWhere("quote_currency = $currency");
}
if ($shippingType > 0) {
	$q->addWhere("qitem_shipping_type = $shippingType");
}
$q->addOrder('qitem_quotation');

$arrLine = $q->loadList();

header('Content-Type: text/xml');

$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');

$xml->startElement('root');
obj2xml($xml, 'lines', 'line', $arrLine);
$xml->endElement();

echo $xml->outputMemory(true);
?>